###################################################################################################
# Legislative Term Limits and Polarization ########################################################
# Michael Olson and Jon Rogowski ##################################################################
###################################################################################################

#  This script generates results presented in Table B.7 in the supplementary materials.

###################################################################################################
# working directory and packages ##################################################################
###################################################################################################
  
  #setwd("./termlimits_polarization_analysisdata")

###################################################################################################
# DIME Data #######################################################################################
###################################################################################################

  dime <- read.csv("dime_aggregate.csv")

###################################################################################################
# Analysis #######################################################################################
###################################################################################################

# Table B.7: Polarization and Term Limits, Campaign Finance Polarization Measure
  
  dime$temp_term_limit <- dime$house_term_limit2
  dime$temp_abs_diff <- dime$abs_diff
  
  dime_nocov <- felm(polarization~temp_term_limit
                     |state+year|0|state,data=dime)
  
  dime_cov1 <- felm(polarization~temp_term_limit
                    +divided_gov+legprofscore+temp_abs_diff
                    |state+year|0|state,data=dime)
  
  dime$temp_term_limit <- dime$house_term_limit2
  dime$temp_abs_diff <- dime$hs_abs_diff
  
  dime_h_nocov <- felm(h_polarization~temp_term_limit
                       |state+year|0|state,data=dime)
  
  dime_h_cov1 <- felm(h_polarization~temp_term_limit
                      +divided_gov+legprofscore+temp_abs_diff
                      |state+year|0|state,data=dime)
  
  dime$temp_term_limit <- dime$senate_term_limit2
  dime$temp_abs_diff <- dime$sen_abs_diff
  
  dime_s_nocov <- felm(s_polarization~temp_term_limit
                       |state+year|0|state,data=dime)
  
  dime_s_cov1 <- felm(s_polarization~temp_term_limit
                      +divided_gov+legprofscore+temp_abs_diff
                      |state+year|0|state,data=dime)
  
  dime_sg <- stargazer(dime_nocov,dime_cov1,dime_h_nocov,dime_h_cov1,dime_s_nocov,dime_s_cov1,
                       add.lines = list(c("State Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                        c("Year Fixed Effects","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark","\\checkmark"),
                                        c("Projected $R^2$",round(summary(dime_nocov)$P.r.squared,3),round(summary(dime_cov1)$P.r.squared,3)
                                          ,round(summary(dime_h_nocov)$P.r.squared,3),round(summary(dime_h_cov1)$P.r.squared,3)
                                          ,round(summary(dime_s_nocov)$P.r.squared,3),round(summary(dime_s_cov1)$P.r.squared,3))),
                       notes.append = FALSE,notes.label = "",
                       notes="\\parbox[t]{0.875\\textwidth}{\\footnotesize \\textit{Note}: Entries are linear regression coefficients with
                         standard errors clustered on states in parentheses.
                         $^{**}$p$<$0.05, $^*$p$<$0.10 (two-tailed test).}",
                       star.char=c("*","**"),star.cutoffs = c(0.10,0.05),digits=3,digits.extra=0,
                       omit.stat = c("rsq", "f", "ser","adj.rsq"),
                       dep.var.caption = c("Legislative Polarization (DIME Outcome)"),
                       dep.var.labels = c("Pooled","House","Senate"),
                       covariate.labels=c("Term Limits","Divided Gov.","ln(Leg. Professionalism)","Party Competitiveness"),
                       label="dime_analysis",
                       table.layout ="-ld-#-t-as-n",table.placement = "!ht",
                       title="Term Limits and Polarization: Campaign Finance Polarization Measure")
  
  cat(dime_sg, sep = '\n', file = paste(output_path,"dime.tex",sep=""))
  
# reset working directory
  
  setwd(storewd)
  
